package sw6.digipecs.widget.user;

import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.exceptions.NoViewFoundException;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.widget.AnimationLayout;
import sw6.digipecs.widget.BitmapImageView;
import sw6.digipecs.widget.DragableExtImageAdapter;
import sw6.digipecs.widget.ExtImageGridView;
import sw6.digipecs.widget.ExtImageView;
import sw6.digipecs.widget.dragdrop.DragLayout;
import sw6.digipecs.widget.dragdrop.DragSource;
import sw6.digipecs.widget.dragdrop.DropTarget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

public class PECSBookPageView extends ExtImageGridView implements DragSource, DropTarget {
	
	private PECSBook mBook;
	private Context mContext;
	private final Rect mRect = new Rect();
	private AnimationLayout mAnimationLayout;

	public PECSBookPageView(Context context) {
		super(context);
		
		mContext = context;
	}

	public PECSBookPageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mContext = context;
	}

	public PECSBookPageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		mContext = context;
	}
	
	public void setBookAndDragLayout(PECSBook book, DragLayout dragLayout) {
		final DragableExtImageAdapter adapter = new DragableExtImageAdapter(mContext, book.getCurrentPage(), dragLayout);
		book.addOnChangeListener(adapter);
		setAdapter(adapter);
		
		mBook = book;
	}
	
	public void setAnimationLayer(AnimationLayout al) {
		mAnimationLayout = al;
	}

	@Override
	public void onSuccessfulDrop(View v, DropTarget target) {
		final Rect rect = mRect;
		getHitRect(rect);
		
		if (target instanceof SentenceBoardLayout) {
			TranslateAnimation animation = new TranslateAnimation(-getWidth(), 0, 0, 0);
			animation.setDuration(250);
			v.startAnimation(animation);
		}
	}

	@Override
	public boolean drop(View fromView, int x, int y) {
		// TODO ...
		
		// If no animation layout has been set, then we do not do anything
		//TODO Shouldn't they be moved anyways ? (just without animation) 
		if (mAnimationLayout == null) return false;
		
		try {
			View toView = findView(x, y);
			
			if (toView instanceof ViewGroup) {
				toView = ((ViewGroup) toView).getChildAt(0);
			}
			
			if (fromView instanceof ExtImageView && toView instanceof ExtImageView) {
				final ExtImageView fromExt = (ExtImageView) fromView;
				final ExtImageView toExt = (ExtImageView) toView;
				
				// Create bitmaps for animation
				fromExt.buildDrawingCache();
				final Bitmap fromBitmap = Bitmap.createBitmap(fromExt.getDrawingCache());
				final BitmapImageView fromBiv = new BitmapImageView(mContext, fromBitmap);
				toExt.buildDrawingCache();
				final Bitmap toBitmap = Bitmap.createBitmap(toExt.getDrawingCache());
				final BitmapImageView toBiv = new BitmapImageView(mContext, toBitmap);
				
				// Create listener
				AnimationListener al = new AnimationListener() {
					private boolean mStarted = false;
					private boolean mStopable = false;
					@Override
					public void onAnimationStart(Animation animation) {
						synchronized (this) {
							if (!mStarted) {
								fromExt.setVisibility(INVISIBLE);
								toExt.setVisibility(INVISIBLE);
								mStarted = true;
							}
						}
					}
					@Override
					public void onAnimationRepeat(Animation animation) {
					}
					@Override
					public void onAnimationEnd(Animation animation) {
						synchronized (this) {
							if (!mStopable) {
								mStopable = true;
							} else {
								try {
									mBook.swapPosition(fromExt.getImage(), toExt.getImage());
								} catch (ImageIsEmptyException e) { }
							}
						}
					}
				};
				
				// Fix coordinates
				final int bitmapOffsetX = fromBitmap.getWidth()/2;
				final int bitmapOffsetY = fromBitmap.getHeight()/2;
				final int offsetX = getLeft();
				final int offsetY = getTop();
				final ViewGroup toVG = (ViewGroup) toExt.getParent();
				final ViewGroup fromVG = (ViewGroup) fromExt.getParent();
				
				final int toX = toVG.getLeft() + offsetX + toExt.getLeft();
				final int toY = toVG.getTop() + offsetY + toExt.getTop();
				final int fromX = fromVG.getLeft() + offsetX + fromExt.getLeft();
				final int fromY = fromVG.getTop() + offsetY + fromExt.getTop();
				
				// Create animation
				TranslateAnimation toAnimation = new TranslateAnimation(toX, fromX, toY, fromY);
				toAnimation.setDuration(250);
				toAnimation.setAnimationListener(al);
				TranslateAnimation fromAnimation = new TranslateAnimation(x + offsetX - bitmapOffsetX, toX, y + offsetY - bitmapOffsetY, toY);
				fromAnimation.setDuration(250);
				fromAnimation.setAnimationListener(al);
				
				// Start animation
				mAnimationLayout.startAnimation(toBiv, toAnimation, al);
				mAnimationLayout.startAnimation(fromBiv, fromAnimation, al);
				
				return true;
			}
		} catch (NoViewFoundException e) { }
		
		return false;
	}

	@Override
	public void onDragIn(int x, int y) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDragOut() {
		// TODO Auto-generated method stub
	}
	
}
